package com.controller;

import com.entity.Bank;
import com.service.BankService;
import com.util.PageTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@SuppressWarnings("SpringMVCViewInspection")
@Controller
public class BankController {

    @Autowired
    BankService bs;

    @RequestMapping("/insertBank")
    public void insertBank(Bank bank, HttpServletResponse response, HttpServletRequest request) throws IOException {
        System.out.println("---" + bank + "---");
        int i = bs.addBank(bank);
        if (i != 0) {
            jsMessage(response, "成功");
        } else {
//            out.wrint("<script>alert('添加失败');location.href='Index.html' </script>");
        }
    }

    @RequestMapping("/removeMoreBank")
    public void removeMoreBank(Integer[] ids, HttpServletResponse response) throws IOException {
        System.out.println(ids);
        if (ids != null && ids.length > 0) {
            bs.removeMore(ids);
        }
        response.setContentType("text/html;charset=utf-8");
        response.setCharacterEncoding("utf-8");
        PrintWriter out = response.getWriter();
        out.write("<script>alert('删除成功');location.href='getBankList'</script>");
//        return "redirect:/getBankList";

    }

    @RequestMapping("/getBankListByPage")
    public String getBankListByPage(Model model, String currentPageNo) throws IOException {
        PageTool pageTool = new PageTool();
        int count = bs.getBankCount();
        pageTool.setTotalCount(count);
        pageTool.setCurrentPageNo(currentPageNo);

        List<Bank> list = bs.findBankListByPage((pageTool.getCurrentPageNo() - 1) * 5, pageTool.getPageSize());
        model.addAttribute("bankList", list);
        model.addAttribute("pageTool", pageTool);
        return "BankList";
    }

    @RequestMapping("/getBankListByCondition")
    public String getBankListByCondition(Model model, String currentPageNo, String name, String produceTime, String cardID) throws IOException {
        Map params = new HashMap<>();
        params.put("customerName", name);
        params.put("produceTime", produceTime);
        params.put("cardID", cardID);
        PageTool pageTool = new PageTool();
        int count = bs.getBankCountByCondition(params);
        pageTool.setTotalCount(count);
        pageTool.setCurrentPageNo(currentPageNo);
        params.put("startIndex", (pageTool.getCurrentPageNo() - 1) * 5);
        params.put("pageSize", pageTool.getPageSize());
        List<Bank> list = bs.findBankByCondition(params);
        model.addAttribute("bank", params);
        model.addAttribute("bankList", list);
        model.addAttribute("pageTool", pageTool);
        return "BankList";
    }

    @RequestMapping("/getBankList")
    public String getBankList(Model model) throws IOException {
        List<Bank> list = bs.findBankList();
        model.addAttribute("bankList", list);
        return "BankList";
    }

    @ResponseBody
    @RequestMapping("/removeBankById")
    public int removeBankById(Integer id) throws IOException {
        int num = bs.removeByPrimaryKey(id);
        return num;
    }

    @ResponseBody
    @RequestMapping("/readyEdit")
    public Bank readyEdit(Integer id) throws IOException {
        Bank bank = bs.findByPrimaryKey(id);
        return bank;
    }

    @ResponseBody
    @RequestMapping("/doEdit")
    public void doEdit(HttpServletRequest request) throws IOException, ParseException {
        System.out.println(111);
        Integer id = Integer.parseInt(request.getParameter("id"));
        String customerName = request.getParameter("customerName");
        BigDecimal currentMoney = new BigDecimal(request.getParameter("currentMoney"));
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date produceTime = format.parse(request.getParameter("produceTime"));
        String cardID = request.getParameter("cardID");
        Bank bank = new Bank();
        bank.setId(id);
        bank.setProduceTime(produceTime);
        bank.setCardID(cardID);
        bank.setCurrentMoney(currentMoney);
        bank.setCustomerName(customerName);
        System.out.println(bank);
        int i = bs.modifytBankById(bank);
    }


    private void jsMessage(HttpServletResponse response, String mes) throws IOException {
        response.setContentType("text/html;charset=utf-8");
        response.setCharacterEncoding("utf-8");
        PrintWriter out = response.getWriter();
        out.write("<script>alert('添加账户" + mes + "');location.href='Index.html'</script>");
    }

//    @RequestMapping("/hello")
//    public String helloHtml(Map<String, Object> map) {
//
//        System.out.println("111");
//        map.put("hello", "from TemplateController.helloHtml");
////        List<String> list = Arrays.asList("张三", "李四", "王五");
//        List<Bank> list = bs.findBankList();
//        map.put("list", list);
//        return "success";
//    }

}
